Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	//定义查询框
	var formPanel = new Ext.form.FormPanel({
		region: 'north',
		labelWidth: 75, // label settings here cascade unless overridden
		frame: true,
		border: false,
		height: 100,
		title: '类型管理',
		labelAlign: 'right',
		buttonAlign: 'center',
		defaults: {
			width: 230
		},
		defaultType: 'textfield',
		items: [{
			fieldLabel: '消费类型',
			name: 'spendtype',
			id: 'spendtype'
		}],
		buttons: [{
			text: '查询',
			iconCls: 'query',
			handler: function(){
				search();
			}
			
		}, {
			text: '重置',
			iconCls: 'reset',
			handler: function(){
				formPanel.getForm().reset();
			}
			
		}]
	});
	//定义行选择
	var sm = new Ext.grid.CheckboxSelectionModel();
	
	//定义工具栏	
	var bar = new Ext.Toolbar({
		buttons: [{
			text: '新增',
			iconCls: 'add',
			handler: add
		}, {
			text: '修改',
			iconCls: 'modify',
			handler: modify
		}, {
			text: '删除',
			iconCls: 'delete',
			handler: remove
		}]
	});
	
	//定义数据源store
	var store = new Ext.data.JsonStore({
		url: "spendtype!getType.action",
		root: 'result',
		totalProperty:'count',
		fields: ['id', 'name', 'creatby', 'time']
	});
	
	//定义分页栏
	var pageBar = new Ext.PagingToolbar({
		pageSize: 5,
		store: store,
		displayInfo: true,
		displayMsg: '显示第 {0} 条到第 {1} 条记录，共 {2} 条',
		emptyMsg: "没有记录"
	})
	store.on('beforeload', function(){
		store.baseParams = {
			typeName: Ext.getCmp('spendtype').getValue()
		};
	});
	//默认查询一次
	search();
	
	//定义数据展示区域panel
	var grid = new Ext.grid.GridPanel({
		region: 'center',
		autoLoad: false,
		frame: true,
		border: false,
		tbar: bar,
		bbar: pageBar,
		loadMask: true,
		cm: new Ext.grid.ColumnModel([new Ext.grid.RowNumberer({
			width: 30
		}), sm, {
			dataIndex: 'id',
			header: '编号',
			hidden: true,
			sortable: true
		}, {
			dataIndex: 'name',
			header: '名称',
			hidden: false,
			sortable: true
		}, {
			dataIndex: 'creatby',
			header: '创建人',
			hidden: false,
			sortable: true
		}, {
			dataIndex: 'time',
			header: '创建日期',
			hidden: false,
			sortable: true
		}]),
		sm: sm,
		store: store //数据源
	});
	//定义界面
	var viewPort = new Ext.Viewport({
		layout: 'border',
		items: [formPanel, grid]
	});
	
	//定义新增页面
	var addWin = new Ext.Window({
		title: '添加类型',
		frame: true,
		layout: 'form',
		labelWidth: '95',
		width: 300,
		height: 150,
		modal: true,
		collapsible: true,
		labelAlign: 'center',
		buttonAlign: 'center',
		bodyStyle: 'padding:25px;',
		items: [{
			xtype: 'textfield',
			fieldLabel: '消费类型',
			allowBlank: false,
			blankText: '请输入消费类型名称!',
			name: 'addTypeName',
			id: 'addTypeName'
		}],
		buttons: [{
			text: '保存',
			iconCls: 'save',
			handler: addSave
		}, {
			text: ' 取消',
			iconCls: 'cancel',
			handler: function(){
				addWin.hide();
			}
		}]
	});
	
	//定义修改页面
	var modifyWin = new Ext.Window({
		title: '修改类型',
		frame: true,
		layout: 'form',
		labelWidth: '95',
		width: 300,
		height: 150,
		modal: true,
		collapsible: true,
		labelAlign: 'center',
		buttonAlign: 'center',
		bodyStyle: 'padding:25px;',
		items: [{
			xtype: 'textfield',
			fieldLabel: '消费类型',
			allowBlank: false,
			blankText: '请输入消费类型名称!',
			name: 'modifyTypeName',
			id: 'modifyTypeName'
		}],
		buttons: [{
			text: '保存',
			iconCls: 'save',
			handler: modifySave
		}, {
			text: ' 取消',
			iconCls: 'cancel',
			handler: function(){
				modifyWin.hide();
			}
		}]
	});
	
	
	/**
	 * 查找
	 */
	function search(){
		store.load({
			params: {
				start: 0,
				limit: 5
			}
		});
	}
	/**
	 * 新增
	 */
	function add(){
		addWin.show();
	}
	/**
	 * 修改
	 */
	function modify(){
		var recs = grid.getSelectionModel().getSelections();
		if (recs.length == 0) {
			Ext.Msg.alert('提示', '请选择需要修改的记录！');
		}
		else 
			if (recs.length > 1) {
				Ext.Msg.alert('提示', '一次只能选择一条记录！');
			}
			else {
				var val = recs[0].get('name');
				Ext.getCmp('modifyTypeName').setValue(val);
				modifyWin.show();
			}
	}
	
		/**
		 * 删除
		 */
		function remove(){
			var recs = grid.getSelectionModel().getSelections();
			if (recs.length == 0) {
				Ext.Msg.alert('提示', '请选择需要删除的记录！');
			}
			else {
				var v_id = '';
				for (i = 0; i < recs.length; i++) {
					if (i == recs.length - 1) {
						v_id += recs[i].get('id');
					}
					else {
						v_id += recs[i].get('id') + ',';
					}
				}
				jQuery.post("spendtype!delType.action", {
					sid: v_id
				}, function(data_c){
					if (data_c.result == 'true') {
						Ext.Msg.alert('提示', '删除消费类型成功!', function(){
							search();
						});
					}
					else 
						if (data_c.result == 'false') {
							Ext.Msg.alert('提示', '删除消费类型失败!', function(){
								search();
							});
						}
				}, "json");
			}
		}
	
	function addSave(){
		var typeName = Ext.getCmp("addTypeName").getValue();
		if (typeName.length > 0 && typeName.trim().length > 0) {
			jQuery.post("spendtype!addType.action", {
				typeName: typeName
			}, function(data_c){
				if (data_c.result == 'true') {
					Ext.Msg.alert('提示', '添加消费类型成功!', function(){
						Ext.getCmp("addTypeName").reset();
						addWin.hide();
						search();
					});
				}
				else 
					if (data_c.result == 'false') {
						Ext.Msg.alert('提示', '添加消费类型失败!', function(){
							Ext.getCmp("addTypeName").reset();
							addWin.hide();
							search();
						});
					}
					else {
						Ext.Msg.alert('提示', '消费类型名称已存在,请重新输入!', function(){
							Ext.getCmp("addTypeName").reset();
						});
					}
			}, "json");
		}
		
	}
	
	
	
	function modifySave(){
		var typeName = Ext.getCmp('modifyTypeName').getValue();
		var rec = grid.getSelectionModel().getSelections();
		var oldName = rec[0].get('name');
		var v_id = rec[0].get('id');
		if (typeName == oldName) {
			Ext.Msg.alert('提示', '请输入修改后的类型名称!');
			return;
		}
		else 
			if (typeName.length > 0 && typeName.trim().length > 0) {
				jQuery.post("spendtype!modifyType.action", {
					sid: v_id,
					typeName: typeName
				}, function(data_c){
					if (data_c.result == 'true') {
						Ext.Msg.alert('提示', '修改消费类型成功!', function(){
							modifyWin.hide();
							search();
						});
					}
					else 
						if (data_c.result == 'false') {
							Ext.Msg.alert('提示', '修改消费类型失败!', function(){
								modifyWin.hide();
								search();
							});
						}
						else {
							Ext.Msg.alert('提示', '消费类型名称已存在,请重新输入!', function(){
								Ext.getCmp("modifyTypeName").reset();
							});
						}
				}, "json");
			}
	}
});
